package org.zfes.snowier.cloud.zbss.cms.tag.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.cms.tag.service.ICmsTagService;
import org.zfes.snowier.cloud.zbss.cms.tag.dao.CmsTagMapper;
import org.zfes.snowier.cloud.zbss.cms.tag.model.CmsTag;

import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
public class CmsTagServiceImpl implements ICmsTagService{
 	@Autowired
	private CmsTagMapper cmsTagMapper;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createCmsTag(CmsTag cmsTag) {
		ZBeanUtil.validateBean(cmsTag);
		cmsTag.setStatus(Byte.valueOf("1"));
		cmsTag.setCreateTime(ZDateUtil.newDate());
		cmsTag.setUpdateTime(ZDateUtil.newDate());
		cmsTagMapper.insertSelective(cmsTag);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsTag> loadCmsTagById(Long id) {
		if(id!=null){
			return Optional.ofNullable(cmsTagMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateCmsTag(CmsTag cmsTag) {
		ZBeanUtil.validateBean(cmsTag);
		cmsTag.setUpdateTime(ZDateUtil.newDate());
		cmsTagMapper.updateByPrimaryKeySelective(cmsTag);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteCmsTag(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		cmsTagMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsTagStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsTagMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadCmsTagDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(cmsTagMapper.selectListCount(pm), cmsTagMapper.selectMapListPage(pm));
		return ds;
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsTag> loadCmsTagListByTagName(String tagName,Long siteId) {
		ZAssert.notNull(tagName, "tagName不能为空");
		ZAssert.notNull(siteId, "siteId不能为空");
		CmsTag cmsTag = cmsTagMapper.selectByTagName(tagName,siteId);
		
		return Optional.ofNullable(cmsTag);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void makeRefCountTotal(String tagName,Long siteId) {
		ZAssert.notNull(tagName, "tagName不能为空");
		ZAssert.notNull(siteId, "siteId不能为空");
		
		cmsTagMapper.makeRefCountTotal(tagName,siteId);
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void subtractRefCountTotal(String tagIds) {
		ZAssert.notNull(tagIds, "请选择tagIds");
		ZAssert.hasText(tagIds, "请填写tagIds");
		
		//取出tagId列表
		List<Long> tagIdList=Arrays.asList(tagIds.split(",")).stream().map(s->Long.parseLong(s.trim())).collect(Collectors.toList());
		for(int i=0;i<tagIdList.size();i++) {
			cmsTagMapper.subtractRefCountTotal(tagIdList.get(i));
		}
		
	}
	
}